package cc.mrbird.assets.service.impl;

import cc.mrbird.assets.dao.AssetTypeMapper;
import cc.mrbird.assets.dao.ParkMapper;
import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.assets.domain.Park;
import cc.mrbird.assets.service.AssetTypeService;
import cc.mrbird.assets.service.ParkService;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("parkService")
public class ParkServiceImpl extends BaseService<Park> implements ParkService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ParkMapper parkMapper;
    @Override
    public List<Park> findAllList(Park park, QueryRequest request) {
        try {
            Example example = new Example(Park.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("park_id");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public Park findByName(String park_name) {
        Example example = new Example(Park.class);
        example.createCriteria().andCondition("lower(park_name)=", park_name.toLowerCase());
        List<Park> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void add(Park park) {
        this.save(park);
    }

    @Override
    public void update(Park park) {
        this.updateNotNull(park);
    }

    @Override
    public Park findById(Long id) {
        return this.selectByKey(id);
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "park_id", Park.class);
    }
}
